package com.googlecode.gwt_control.client.form;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author Olafur Gauti Gudmundsson
 */
public class CheckBoxField extends Field {

    private CheckBox box;

    public CheckBoxField( String id, String title ) {
        this(id, title, null);
    }

    public CheckBoxField( String id, String title, ClickListener clickListener ) {
        super(id, "", null);

        box = new CheckBox(title);
        box.setStyleName("checkBoxField");
        box.addClickListener(new ClickListener() {
            public void onClick(Widget w) {
                fireFieldChanged();
            }
        });
        if ( clickListener != null ) {
            box.addClickListener(clickListener);
        }
    }

    @Override
    public void addToTable(int rowNumber, FlexTable table) {
        super.addToTable(rowNumber, table, box);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        box.setEnabled(enabled);
        if ( enabled ) {
            box.removeStyleName("disabled");
        } else {
            box.addStyleName("disabled");
        }
    }

    public CheckBox getBox() {
        return box;
    }

    public boolean isChecked() {
        return box.isChecked();
    }

    public void setChecked( boolean checked ) {
        box.setChecked(checked);
    }

    @Override
    public boolean isEmpty() {
        return !box.isChecked();
    }

}
